package com.tianqu.tms.www.transfer.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.FieldChecker;
import com.tianqu.tms.entity.TransferAcceptedInfo;

/**
 * @author mufeiyu
 * 
 */
public class CarTransferInfoAjaxAction extends TmsAjaxAction{
	private Logger logger = Logger.getLogger(CarTransferInfoAjaxAction.class);
	private static final String SERIAL = "serial";
	private static final String LICENSEPLATE = "licensePlate";
	private static final String VIN = "vin";
	private static final String BUSINESSTYPE = "businessType";

	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		String serial = request.getParameter(SERIAL);
		String businessType = request.getParameter(BUSINESSTYPE);
		String vin = request.getParameter(VIN);

		String errorFlag = "0";
		TransferAcceptedInfo transferInfo = TransferAcceptedInfo.getInstance();
		
		if("1".equals(businessType)){
			String licensePlate = request.getParameter(LICENSEPLATE);
			try {
				licensePlate = URLDecoder.decode(licensePlate,"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String firstValue = licensePlate.substring(0, 1);
			String secondValue = licensePlate.substring(1, 2);
			String otherValue = licensePlate.substring(2, licensePlate.length());
			if (!FieldChecker.checkLength(licensePlate, 7, 7)){
				errorFlag = "1"; // 长度是7位的check
			}else if(FieldChecker.isProvinceSt(firstValue)
					&& FieldChecker.isAlphabetSt(secondValue)
					&& pattern(otherValue,"^[0-9a-zA-Z]{4}[0-9a-zA-Z领学挂]{1}")) {
				if(transferInfo.isExistTransferInfoByBasicCarId(serial)){
					errorFlag = "2";
				}else if(transferInfo.isExistTransferInfoByLicensePlate(licensePlate)){
					errorFlag = "3";
				}else if(transferInfo.isExistTransferInfoByVin(vin)){
					errorFlag = "4";
				}
			}else{
				errorFlag = "1";
			}
		}else if("2".equals(businessType)){			
			
			if(transferInfo.isExistTransferInfoByBasicCarId(serial)){
				errorFlag = "2";
			}else if(transferInfo.isExistTransferInfoByVin(vin)){
				errorFlag = "4";
			}
		}
		
		String responseText = errorFlag;
		controller.commit();
		responseValues(response, responseText, TEXT);
		
		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}
	/**
	 * 正则表达式
	 * @return
	 */
	private static boolean pattern(String value,String regex){
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(value);
		return m.matches();
	}
}
